package com.douyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.douyang.VO.FollowedUserInfoVO;
import com.douyang.base.expcetion.UserException;
import com.douyang.base.model.UserBaseInfo;
import com.douyang.base.utils.UserUtil;
import com.douyang.mapper.UserFollowedMapper;
import com.douyang.pojo.UserFollowed;
import com.douyang.service.UserBaseInfoService;
import com.douyang.service.UserFollowedService;
import jakarta.security.auth.message.AuthException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2025-03-05
 */
@Service
public class UserFollowedServiceImpl extends ServiceImpl<UserFollowedMapper, UserFollowed> implements UserFollowedService {

    @Autowired
    private UserBaseInfoService userBaseInfoService;


    @Override
    public void followedAuthor(Long authorId) {
        UserBaseInfo loginUserBaseInfo = UserUtil.getLoginUserBaseInfo();
        boolean exists = lambdaQuery()
                .eq(UserFollowed::getUserId, loginUserBaseInfo.getId())
                .eq(UserFollowed::getFollowedUserId, authorId)
                .exists();

        if (exists) {return;}

        UserFollowed userFollowed = UserFollowed.builder()
                .userId(loginUserBaseInfo.getId())
                .followedUserId(authorId)
                .createTime(LocalDateTime.now())
                .build();
        save(userFollowed);
    }

    @Override
    public List<FollowedUserInfoVO> getFollowedUserInfoList() {
        UserBaseInfo loginUserBaseInfo = UserUtil.getLoginUserBaseInfo();
        List<UserFollowed> userFollowedList = lambdaQuery()
                .eq(UserFollowed::getUserId, loginUserBaseInfo.getId())
                .list();
        if(CollectionUtils.isEmpty(userFollowedList)){return List.of();}
        List<Long> userFollowedListIds = userFollowedList.stream().map(UserFollowed::getFollowedUserId).toList();


        Map<Long, UserBaseInfo> collect = userBaseInfoService.lambdaQuery()
                .in(UserBaseInfo::getId, userFollowedListIds)
                .list()
                .stream()
                .collect(Collectors.toMap(UserBaseInfo::getId, userBaseInfo -> userBaseInfo));

        return userFollowedList.stream().map(userFollowed -> {
            UserBaseInfo userBaseInfo = collect.get(userFollowed.getFollowedUserId());
            if (userBaseInfo == null) {return null;}
            FollowedUserInfoVO followedUserInfoVO =new FollowedUserInfoVO();
            BeanUtils.copyProperties(userBaseInfo,followedUserInfoVO);
            followedUserInfoVO.setCreateTime(userFollowed.getCreateTime());
            return followedUserInfoVO;
        }).toList();
    }

    @Override
    public void unFollowed(Long userId) {
        if(userId==null)throw new UserException("unFollowed:参数异常");
        UserBaseInfo loginUserBaseInfo = UserUtil.getLoginUserBaseInfo();
        remove(new QueryWrapper<UserFollowed>()
                .eq("user_id", loginUserBaseInfo.getId())
                .eq("followed_user_id",userId)
        );
    }
}
